<ol class="am-breadcrumb">
    <li><a href="#">Task管理</a></li>
    <li class="am-active">单次执行</li>
</ol>
<div class="tpl-portlet-components">
    <div class="portlet-title">
        <div class="caption font-green bold">
            <span class="am-icon-code"></span> 脚本
        </div>
    </div>
    <div class="tpl-block ">
        <div class="am-g tpl-amazeui-form">
            <div class="am-u-sm-6 am-u-md-6">
                <form class="am-form am-form-horizontal">
                    <div class="am-form-group">
                        <label for="computer-ssh-port" class="am-u-sm-3 am-form-label">标签</label>
                        <div class="am-u-sm-9">
                            <select id="computer">
                                {% for asset in assets %}
                                    <option value="{{ asset.id }}">{{ asset.network_ip }} {{ asset.hostname }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <label for="computer-remark" class="am-u-sm-3 am-form-label">备注</label>
                        <div class="am-u-sm-9">
                            <textarea class="" rows="13" id="shell" placeholder="命令"></textarea>
                        </div>
                    </div>

                    <div class="am-form-group">
                        <div class="am-u-sm-9 am-u-sm-push-3">
                            <button type="button" class="am-btn am-btn-primary" id="invoke_shell">执行</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="am-u-sm-6 am-u-md-6">
                <pre id="shell_result" class="am-pre-scrollable" style="min-height: 400px;">
                </pre>
            </div>
        </div>
    </div>
</div>
<script>
    $("#invoke_shell").click(function () {
        let assetId = $("#computer").val();
        let shell = $("#shell").val();
        if (shell.length == 0) {
            alert('请输入 shell');
            return;
        }

        $.ajax({
            url: "task/invoke_shell",
            data: {
                "csrfmiddlewaretoken": $("input[name='csrfmiddlewaretoken']").val(),
                "assetId": assetId,
                "shell": shell
            },
            type: 'POST',
            success: function (data) {
                if (data.code === 200) {
                    $("#shell_result").html(data.msg);
                } else {
                }
            },
            error: function () {
            }
        })
    })
</script>